# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=reportlab
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.5.32
pkgrel=1
pkgdesc="A proven industry-strength PDF generating solution (mingw-w64)"
arch=('any')
license=('custom')
url="https://bitbucket.org/rptlab/reportlab"
depends=("${MINGW_PACKAGE_PREFIX}-freetype")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python2-pip"
             "${MINGW_PACKAGE_PREFIX}-python3-pip"
             "${MINGW_PACKAGE_PREFIX}-python2-Pillow"
             "${MINGW_PACKAGE_PREFIX}-python3-Pillow")
source=(https://files.pythonhosted.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz
        "0001-Do-not-specialcase-quote-LIBART_VERSION-on-Windows.patch")
sha256sums=('83ef44936ef4e9c432d62bc2b72ec8d772b87af319d123e827a72e9b6884c851'
            '084b3574f7cb83dc7c9412b9976d672fda0f5f20cef6c6f6b66a6cefc31220ef')

prepare() {
  pushd ${_realname}-${pkgver}
    patch -p1 < ${srcdir}/0001-Do-not-specialcase-quote-LIBART_VERSION-on-Windows.patch
  popd
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
  done
}

build() {
  export FT_LIB=${MINGW_PREFIX}/lib/libfreetype.a
  export FT_INC=${MINGW_PREFIX}/include/freetype2
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done 
}

check() {
#note that a few don't work.
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
#    cd "${srcdir}/python${pver}-build-${CARCH}"
#    ${MINGW_PREFIX}/bin/python${pver} setup.py tests
  done
}

package_python2-reportlab() {
  depends+=("${MINGW_PACKAGE_PREFIX}-python2-pip"
            "${MINGW_PACKAGE_PREFIX}-python2-Pillow")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build


  install -Dm644 src/reportlab/license.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python2-${_realname}/LICENSE
}

package_python3-reportlab() {
  depends+=("${MINGW_PACKAGE_PREFIX}-python3-pip"
            "${MINGW_PACKAGE_PREFIX}-python3-Pillow")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 src/reportlab/license.txt \
    "${pkgdir}${MINGW_PREFIX}"/share/licenses/python3-${_realname}/LICENSE
}

package_mingw-w64-i686-python2-reportlab() {
  package_python2-reportlab
}

package_mingw-w64-i686-python3-reportlab() {
  package_python3-reportlab
}

package_mingw-w64-x86_64-python2-reportlab() {
  package_python2-reportlab
}

package_mingw-w64-x86_64-python3-reportlab() {
  package_python3-reportlab
}
